package ModelLayer;
import java.util.ArrayList;

/**
 * The class DVDContainer holds all of our DVD's in a collection.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<DVD> dvds;
    private static DVDContainer dvdContainer;

    /**
     * Constructor for objects of class DVDContainer
     * creates an array that stores dvds
     */
    private DVDContainer()
    {
        // initialise instance variables
        dvds = new ArrayList<DVD>();
    }
    
    /**
     * singleton dvdcontainer
     */
    public static DVDContainer getDVDContainer()
    {
        if(dvdContainer == null)
        {
            dvdContainer = new DVDContainer();
        }
        return dvdContainer;
    }

    /**
     * Adding a DVD to the colletion
     * 
     * @param newDVD - the objectname of the DVD
     */
    public void addDVD(DVD newDVD)
    {
        dvds.add(newDVD);
    }

    /**
     * returns a dvd searched by its idnumber
     */
    public DVD searchDVD(int id)
    {
        boolean found = false;
        int index = 0;
        while(index < dvds.size() && !found)
        {
            if((dvds.get(index).getId()) == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found == true)
        {
            return dvds.get(index);
        }
        else
        {
            return null;
        }
    }
    
   

    /**
     * Update a DVD
     * 
     * @param id - get the DVD by the id
     * @param title - update the title
     * @param artist - update the title
     * @param title - update the title
     */
    public void updateDVD(int id, String title, String artist, String releaseDate)
    {
        DVD dvd = searchDVD(id);
        if(dvd != null)
        {
            dvd.setTitle(title);
            dvd.setArtist(artist);
            dvd.setReleaseDate(releaseDate);
        }
    }
    
    /**
     * deletes a dvd found by its idnumber
     */
    public void deleteDVD(int id)
    {
        DVD dvd = searchDVD(id);
        if(dvd != null)
        {
            dvds.remove(dvd);
        }
    }
    
    /**
     * prints info on all dvds and their copies
     */
    public void listAllDVDs()
    {
        if(!dvds.isEmpty())
        {
            for(int index = 0; index < dvds.size(); index++)
            {
                dvds.get(index).printInfo();
                dvds.get(index).listAllCopies();
                
            }
        }
        else
        {
            System.out.println("There are no DVDs in the list");
        }
    }
}
